import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import NotFundPage from '../views/404.vue'
import LayOut from '../Layout/index.vue'
import ThreeMenuRoute from '../components/MenuRoute.vue'
import { storageLocal, storageSession } from '@/storage/index'
import { warnMessage } from '@/utils/messageTips'
const routes: Array<RouteRecordRaw> = [
  // 首页、面板
  {
    path: '/',
    name: 'Welcom',
    component: LayOut,
    redirect: '/welcom/first',
    children: [
      {
        path: '/welcom/first',
        name: 'First',
        component: () => import('@/views/PageOne.vue'),
        meta: {
          title: '数据面板',
          showLink: true,
          icon: 'DataBoard'
        }
      },
    ],
    meta: {
      title: '首页',
      showLink: true
    }
  },

  // 物资管理

  {
    path: '/goods',
    component: LayOut,
    redirect: '/goods/goodsinfo',
    children: [

      {
        path: '/goods/goodsinfo',
        name: 'GoodsInfo',
        component: () => import('@/views/GoodsInfo.vue'),
        meta: {
          title: '物资信息',
          showLink: true,
          icon: 'goodsFilled'
        }
      },
    ],
    meta: {
      title: "物资管理",
      showLink: true,

    }
  },

  // 场地管理
  {
    path: '/order',
    component: LayOut,
    redirect: '/order/orderlist',
    children: [
      {
        path: '/order/orderlist',
        name: 'OrderList',
        component: () => import('@/views/PlaceList.vue'),
        meta: {
          title: '场地信息',
          showLink: true,
          icon: 'List'
        }
      }
    ],
    meta: {
      title: '场地管理',
      showLink: true,

    }
  },

  // 校长
  {
    path: '/school',
    component: LayOut,
    redirect: '/school/master',
    children: [

      {
        path: '/school/master',
        name: 'SchoolMasterPage',
        component: () => import('@/views/SchoolMaster.vue'),
        meta: {
          title: '校级办公',
          showLink: true,
          icon: 'InfoFilled'
        }
      },
    ],
    meta: {
      title: "业务处理",
      showLink: true
    }
  },

  // 院级办公
  {
    path: '/school',
    component: LayOut,
    redirect: '/school/second',
    children: [

      {
        path: '/school/second',
        name: 'SchoolSecondPage',
        component: () => import('@/views/SchoolSecond.vue'),
        meta: {
          title: '院级办公',
          showLink: true,
          icon: 'InfoFilled'
        }
      },
    ],
    meta: {
      title: "业务处理",
      showLink: true
    }
  },



  // 登录信息
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: {
      title: "登录页面",
      showLink: false
    }
  },
  // 错误页面
  {
    path: '/404',
    component: NotFundPage,
    meta: {
      title: 'NOT FOUND',
      showLink: false
    }
  },
]

const allowBaseRoutes = ['/404', '/login']

const router = createRouter({
  // 创建web哈希页面就是前面有个#
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  const authorityRoutes = storageSession.getItem('routes')
  document.title = to.meta.title as string
  // 数组不为空且长度不为0说明登录了
  if (authorityRoutes && authorityRoutes.length !== 0) {
    const accessRouteArray = [...allowBaseRoutes, ...authorityRoutes]
    accessRouteArray.includes(to.path) ? next() : next('/')
  } else {
    allowBaseRoutes.includes(to.path) ? next() : next('/login')
  }
})


export default router
